<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class WorkLog extends \app\BaseModel
{
    protected $name = "massage_coach_work_log";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function initData($coach_id)
    {
        $coach_model = new Coach();
        $now_time = time();
        $coach = $coach_model->dataInfo(["id" => $coach_id]);
        $dis = ["coach_id" => $coach_id, "status" => 2];
        $day_start = strtotime(date("Y-m-d"), $now_time);
        $last_time = $this->where($dis)->max("create_time");
        $is_work = 1;
        if(!empty($last_time) && $day_start - 86400 <= $last_time) {
            $is_work = 0;
        }
        if($coach["is_work"] != 1 || $coach["status"] != 2) {
            $is_work = 0;
        }
        $last_time = !empty($last_time) && 0 < $last_time ? $last_time + 86400 : $coach["sh_time"];
        if($last_time <= 0) {
            return false;
        }
        $i = 0;
        while ($last_time < $day_start) {
            $true_last_time = $last_time;
            if($i == 0) {
                $ing = $this->where(["coach_id" => $coach_id, "status" => 1])->order("id desc")->find();
                $ing = !empty($ing) ? $ing->toArray() : [];
                $true_last_time = !empty($ing) ? $ing["true_time"] : $true_last_time;
                $time = $this->getHeadTimeData($coach["start_time"], $coach["end_time"], $true_last_time, strtotime(date("Y-m-d", $true_last_time)) + 86400);
                $time = $is_work == 1 ? $time : 0;
                if(!empty($ing)) {
                    $time += $ing["time"];
                    $time_list_model = new CoachTimeList();
                    $rest_time = $time_list_model->where(["coach_id" => $coach_id, "status" => 0, "is_click" => 1, "is_work" => 1, "time_texts" => date("Y-m-d", $true_last_time)])->field("SUM(time_str_end-time_str) as time_long")->find();
                    $rest_time = $rest_time->time_long;
                    $time = $rest_time < $time ? $time : $rest_time;
                    $this->dataUpdate(["id" => $ing["id"]], ["time" => $time, "true_time" => time(), "start_time" => $coach["start_time"], "end_time" => $coach["end_time"], "status" => 2]);
                    $i++;
                    $last_time += 86400;
                }
            } else {
                $time = $this->getDayWorkTime($coach["start_time"], $coach["end_time"]);
            }
            $time = $is_work == 1 ? $time : 0;
            $insert = ["uniacid" => $coach["uniacid"], "coach_id" => $coach_id, "status" => 2, "date" => date("Y-m-d", $true_last_time), "create_time" => strtotime(date("Y-m-d", $true_last_time)), "time" => $time, "true_time" => $now_time, "start_time" => $coach["start_time"], "end_time" => $coach["end_time"]];
            $this->insert($insert);
            $i++;
            $last_time += 86400;
        }
        return true;
    }

    /**
     * 更新时间在线时间
     * @param $coach_id
     * @param $is_admin
     * @return true
     */
    public function updateTimeOnline($coach_id, $is_admin = 0)
    {
        if(empty($coach_id)) {
            return true;
        }
        $key = $coach_id . "update_online_time_key";
        $now_time = time();
        $coach_model = new Coach();
        $coach = $coach_model->dataInfo(["id" => $coach_id]);
        incCache($key, 1, $coach["uniacid"]);
        $key_value = getCache($key, $coach["uniacid"]);
        $day_start = strtotime(date("Y-m-d"), $now_time);
        $key_values = getCache("update_online_time_key_key" . $coach_id, $coach["uniacid"]);
        if($key_value == 1 && empty($key_values) || $is_admin != 1) {
            setCache("update_online_time_key_key" . $coach_id, 1, 3600, $coach["uniacid"]);
            $this->initData($coach_id);
            if($coach["status"] != 2 || $coach["is_work"] == 0 || empty($coach["start_time"])) {
                $is_work = 0;
            } else {
                $is_work = 1;
            }
            $ing = $this->dataInfo(["coach_id" => $coach_id, "status" => 1, "create_time" => $day_start]);
            if(!empty($ing)) {
                $time = $this->getHeadTimeData($coach["start_time"], $coach["end_time"], $ing["true_time"], $now_time);
                $time = $is_work == 1 ? $time : 0;
                $this->dataUpdate(["id" => $ing["id"]], ["time" => $ing["time"] + $time, "true_time" => $now_time, "start_time" => $coach["start_time"], "end_time" => $coach["end_time"]]);
            } else {
                $day_start = $coach["sh_time"] < $day_start ? $day_start : $coach["sh_time"];
                $time = $this->getHeadTimeData($coach["start_time"], $coach["end_time"], $day_start, $now_time);
                $time = $is_work == 1 ? $time : 0;
                $insert = ["uniacid" => $coach["uniacid"], "coach_id" => $coach_id, "date" => date("Y-m-d", time()), "create_time" => strtotime(date("Y-m-d", time())), "time" => $time, "status" => 1, "true_time" => time(), "start_time" => $coach["start_time"], "end_time" => $coach["end_time"]];
                $this->dataAdd($insert);
            }
            $insert = ["uniacid" => $coach["uniacid"], "coach_id" => $coach_id, "date" => date("Y-m-d", time()), "create_time" => time(), "time" => $time, "start_time" => $coach["start_time"], "end_time" => $coach["end_time"], "is_work" => $coach["is_work"], "is_admin" => $is_admin];
            $time_model = new TimeLog();
            $time_model->dataAdd($insert);
        }
        decCache($key, 1, $coach["uniacid"]);
        return true;
    }
    public function getHeadTimeData($work_start, $work_end, $start, $end)
    {
        if(empty($work_start) || $end < $start) {
            return 0;
        }
        $now_time = time();
        $now = strtotime(date("Y-m-d", $now_time));
        $work_start = strtotime($work_start) - $now;
        $work_end = strtotime($work_end) - $now;
        $time = 0;
        $start_int = strtotime(date("Y-m-d", $start));
        $work_starts = $work_start + $start_int;
        $work_ends = $work_end + $start_int;
        $arr = [];
        if($work_ends <= $work_starts) {
            $star_arr["start_time"] = $work_starts;
            $star_arr["end_time"] = $start_int + 86400;
            $end_arr["start_time"] = $start_int;
            $end_arr["end_time"] = $work_ends;
            $arr[] = $star_arr;
            $arr[] = $end_arr;
        } else {
            $star_arr["start_time"] = $work_starts;
            $star_arr["end_time"] = $work_ends;
            $arr[] = $star_arr;
        }
        foreach ($arr as $value) {
            $true_start = $start < $value["start_time"] ? $value["start_time"] : $start;
            $true_end = $value["end_time"] < $end ? $value["end_time"] : $end;
            $true_time = 0 < $true_end - $true_start ? $true_end - $true_start : 0;
            $time += $true_time;
        }
        return $time;
    }
    public function getHeadTimeDataV2($work_start, $work_end, $start, $end)
    {
        if(empty($work_start)) {
            return 0;
        }
        $now_time = time();
        $now = strtotime(date("Y-m-d", $now_time));
        $work_start = strtotime($work_start) - $now;
        $work_end = strtotime($work_end) - $now;
        $time = 0;
        while ($start + 10 < $end) {
            $start_int = strtotime(date("Y-m-d", $start));
            $work_starts = $work_start + $start_int;
            $work_ends = $work_end + $start_int;
            $arr = [];
            if($work_ends <= $work_starts) {
                $star_arr["start_time"] = $work_starts;
                $star_arr["end_time"] = $start_int + 86400;
                $end_arr["start_time"] = $start_int;
                $end_arr["end_time"] = $work_ends;
                $arr[] = $star_arr;
                $arr[] = $end_arr;
            } else {
                $star_arr["start_time"] = $work_starts;
                $star_arr["end_time"] = $work_ends;
                $arr[] = $star_arr;
            }
            foreach ($arr as $value) {
                if($value["start_time"] <= $start && $start <= $value["end_time"]) {
                    $time += 10;
                }
            }
            $start += 10;
        }
        return $time;
    }
    public function getHeadTime($work_start, $work_end, $start_time)
    {
        $now_time = time();
        $start_int = strtotime(date("Y-m-d", $start_time));
        $end_int = $start_int + 86400;
        $end_int = $now_time < $end_int ? $end_int : $now_time;
        $end_int = $now_time < $end_int ? $end_int : $now_time;
        $now = strtotime(date("Y-m-d", $now_time));
        $work_start = strtotime($work_start) - $now + $start_int;
        $work_end = strtotime($work_end) - $now + $start_int;
        if($work_end <= $work_start) {
            $start = 0 < $work_end - $start_time ? $work_end - $start_time : 0;
            $end = $end_int - $work_start;
            return $start + $end;
        }
        $work_start = $start_time <= $work_start ? $work_start : $start_time;
        return 0 < $work_end - $work_start ? $work_end - $work_start : 0;
    }
    public function getTailTime($work_start, $work_end, $start_time = 0)
    {
        $start_int = strtotime(date("Y-m-d", time()));
        $start_time = !empty($start_time) ? $start_time : $start_int;
        if(!($start_int <= $start_time && $start_time <= time())) {
            return 0;
        }
        $work_start = strtotime($work_start);
        $work_end = strtotime($work_end);
        if($work_end <= $work_start) {
            $start = 0 < $work_end - $start_time ? $work_end - $start_time : 0;
            $work_start = $start_time < $work_start ? $work_start : $start_time;
            $end = 0 < time() - $work_start ? time() - $work_start : 0;
            return $start + $end;
        }
        $work_start = $start_time < $work_start ? $work_start : $start_time;
        $work_end = time() <= $work_end ? time() : $work_end;
        return 0 < $work_end - $work_start ? $work_end - $work_start : 0;
    }
    public function getDayWorkTime($start_time, $end_time)
    {
        if(empty($start_time)) {
            return 0;
        }
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $end_time = $start_time < $end_time ? $end_time : $end_time + 86400;
        $day_time = $end_time - $start_time;
        return $day_time;
    }
}

?>